package t3dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import t3jdbc.JDBCUtil;
import t3bean.ParkingSpaceBean;

public class ParkingSpaceDao extends Dao{
	private SetSql ss=new SetSql(); 
	
	public int update(ParkingSpaceBean psb) {
		String sql="update parkingspace set ";
		List<Object> list=new ArrayList<>();
		sql=ss.setSql(psb, sql, list, 1);
		sql+=" where space_id=?";
//		System.out.println(sql);
		list.add(psb.getSpaceId());
		try {
			return dml(sql,list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public List<ParkingSpaceBean> select(ParkingSpaceBean psb) throws SQLException{
		String sql;
		List<ParkingSpaceBean> backList = new ArrayList<>();
		List<Object> list = new ArrayList<>();
		ResultSet rs = null;
		if (psb != null) {
			sql = "select * from parkingspace where ";
			sql = ss.setSql(psb, sql, list, 2);
//			System.out.println(sql);
		} else {
			sql = "select * from parkingspace";
		}

		try {
			rs = dql(sql, list);
			while (rs.next()) {
				ParkingSpaceBean psb1=new ParkingSpaceBean();
				psb1.setId(rs.getInt("id"));
				psb1.setIsParking(rs.getInt("isparking"));
				psb1.setSpaceId(rs.getInt("space_id"));
				psb1.setCarid(rs.getString("carid"));
				backList.add(psb1);
			}
			return backList;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(super.conn, super.ps, super.rs);
			super.conn = null;
			super.ps = null;
			super.rs = null;
			rs.close();
			rs = null;
		}
		return null;
		
	}
}
